package tp.pr2;



public class ItemContainer {
	
	/**
	 * Creates the empty container
	 */
	public ItemContainer() {
		items = new Vec_thor();
	}
	
	/**
	 * @returns the number of items in the container
	 */
	public int numberOfItems() {
		return this.items.size();
	}
	
	/**
	 * Add an item to the container. If failure, return false
	 * @param item the name of the item to be added
	 * @return true if and only the item is added (there are no itmes with the same name in the container)
	 */
	public boolean addItem(Item item) {
		return this.items.add(item);
	}
	
	/**
	 * returns the item from the container with id = id
	 * @param id item name
	 * @return item with that name or NULL
	 */
	public Item getItem(String id) {
		for (int i = 0; i < this.items.size(); i++)
			if (((Item) this.items.get(i)).getId().equalsIgnoreCase(id))
				return (Item) this.items.get(i);

		return null;
	}
	
	/**
	 * Returns and deletes an item from the inventory. This operation can fail, returning null
	 * @param id Name of the item
	 * @return An item if and only if the item identified by id exists in the inventory. Otherwise it returns null
	 */
	public Item pickItem(String id) {
		Item item = getItem(id);
		if (item != null)
			items.remove(item);
		return item;
	}
	

	
	/**
	 * Generates a String with information about the items contained in the container. Note that the items must appear sorted but the item name.
	 * Returns an empty String "" if the container had no objects
	 */
	public String toString() {

		String str = "";
		for (int i = 0; i < this.items.size(); i++)  {
			str = str + "   " + ((Item) this.items.get(i)).getId();
			if (i != this.items.size() - 1) str = str + Interpreter.LINE_SEPARATOR; //añadimos line separator menos en la última
		}

		return str;
	}
	
	
	private Vec_thor items;	
}
